import {Component, OnInit, ViewChild} from '@angular/core';
import {DeviceGroupService} from './device-group.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {DeviceGroupEditComponent} from './device-group-edit/device-group-edit.component';

@Component({
  selector: 'app-device-group-list',
  templateUrl: './device-group-list.component.html',
  styles: []
})
export class DeviceGroupListComponent implements OnInit {

  loading = false;

  // 设备组数据
  deviceGroups = [];

  // 查询参数
  searchBeans = [{
    attributeName: "code",
    searchType: "LIKE",
    attributeValue: ""
  }, {
    attributeName: "name",
    searchType: "LIKE",
    numberValue: ""
  }];

  // 分页参数
  pageData = {page: 0, size: 10};

  @ViewChild('deviceGroupEditComponent', {static: false})
  public deviceGroupEditComponent: DeviceGroupEditComponent;

  constructor(private deviceGroupSrv: DeviceGroupService,
              private msgSrv: NzMessageService,) {
  }

  ngOnInit(): void {
    this.loadListData();
  }

  delete(id?) {
    this.loading = true;
    this.deviceGroupSrv.delete(id).subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.msgSrv.success('删除完成');
      } else {
        this.msgSrv.error(res.message);
      }
    });
    console.log(id);


  }

  /**
   * 清除查询参数
   */
  clearParam() {

    this.searchBeans.forEach((item) => {
      item.attributeValue = '';
    });
  }

  onSmt() {
    this.loadListData();
  }

  cancel() {
    this.msgSrv.success('已取消');
  }

  openEdit(record ?: any): void {
    this.deviceGroupEditComponent.edit(record);
  }

  open(){
    this.deviceGroupEditComponent.open();
  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: this.searchBeans
      }
    };

    this.deviceGroupSrv.findPage(searchData).subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.deviceGroups = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  _onReuseInit() {
    this.loadListData();
  }

}
